#include "Core.h"
#include "Application.h"
#include <iostream>
#include <filesystem>
#include "Log.h"
#include "Hazel/ImGui/ImGuiLayer.h"
#include "imgui.h"
namespace Hazel
{
    Application *Application::s_Instance = nullptr;
    Application::Application()
    {
        HZ_CORE_ASSERT(!s_Instance, "Application already exists!");
        s_Instance = this;

        // 初始化控制台日志系统
        Log::Init();
        HZ_CORE_INFO("Initializing Hazel Engine!");

        try
        {
            // 测试所有日志级别和宏
            HZ_CORE_TRACE("This is a core trace message");
            HZ_CORE_INFO("This is a core info message");
            HZ_CORE_WARN("This is a core warning message");
            HZ_CORE_ERROR("This is a core error message");
            HZ_CORE_FATAL("This is a core fatal message");

            HZ_TRACE("This is a client trace message");
            HZ_INFO("This is a client info message");
            HZ_WARN("This is a client warning message");
            HZ_ERROR("This is a client error message");
            HZ_FATAL("This is a client fatal message");

            // 测试日志刷新
            Log::Flush();
            HZ_CORE_INFO("Log flush completed");

            HZ_CORE_INFO("File logging initialized successfully");

            HZ_CORE_ASSERT(!s_Instance, "Application already exists!");
            s_Instance = this;
            // 1. 先创建窗口
            m_Window = Window::Create();
            HZ_CORE_ASSERT(m_Window, "Failed to create window!");
            // 2. 设置事件回调
            m_Window->SetEventCallback(HZ_BIND_EVENT_FN(Application::OnEvent));
            // 4. 最后创建 ImGui 层
            m_ImGuiLayer = new ImGuiLayer();
            m_ImGuiLayer->OnAttach(); // 手动调用 OnAttach 来初始化 ImGui 后端
            // PushOverlay(m_ImGuiLayer);
        }
        catch (const spdlog::spdlog_ex &ex)
        {
            HZ_CORE_ERROR("File log initialization failed: {}", ex.what());
        }
        catch (const std::exception &ex)
        {
            HZ_CORE_ERROR("Failed to create log directory: {}", ex.what());
        }
        // 创建窗口（这会创建OpenGL上下文）
        //  m_Window = Window::Create();
        //  m_Window->SetEventCallback(HZ_BIND_EVENT_FN(Application::OnEvent));
        //  // 然后初始化渲染器
        //  Renderer::Init();
    }
    Application::~Application()
    {
    }
    void Application::OnEvent(Event &e)
    {
        EventDispatcher dispatcher(e);
        dispatcher.Dispatch<WindowCloseEvent>(HZ_BIND_EVENT_FN(Application::OnWindowClose));

        // 将事件传递给层栈（从后往前）
        // for (auto it = m_LayerStack.rbegin(); it != m_LayerStack.rend(); ++it)
        // {
        //     if (e.Handled)
        //         break;
        //     (*it)->OnEvent(e);
        // }
    }

    bool Application::OnWindowClose(WindowCloseEvent &e)
    {
        m_Running = false;
        return true;
    }

    void Application::Run()
    {
        while (m_Running)
        {
            // for (Layer* layer : m_LayerStack)
            //     layer->OnUpdate();

            // 开始ImGui帧
            m_ImGuiLayer->Begin();

            // 显示ImGui演示窗口
            static bool show_demo_window = true;
            if (show_demo_window)
                ImGui::ShowDemoWindow(&show_demo_window);

            // 结束ImGui帧并渲染
            m_ImGuiLayer->End();

            // 更新窗口
            m_Window->OnUpdate();
        }
    }
}
